<?php
require_once ('IOperations.php');
require_once ('ILogin.php');
require_once ('AppController.php');



/**
 * @author ValeBonus.com
 * @version 1.0
 * @created 02-dez-2013 23:36:28
 */
class UsersController extends AppController implements IOperations, ILogin {

	/**
	 * Set the controller name
	 */
	public $name = 'Users';

	public function index() {
		// Show all elements
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	public function add() {
		// Check if the request is an post http method
		if ($this->request->is('post')) {
			// Create the user
			$this->User->create();
				
			// Persist the user
			if ($this->User->save($this->request->data)) {
				// Show a user message
				$this->Session->setFlash(__('O usuário foi salvo'), 'flash_success');
		
				// Redirect to the index function
				$this->redirect(array('action' => 'index'));
			} else {
				// Show a user message
				$this->Session->setFlash(__('O usuário não pode ser salvo. Por favor, tente de novo'), 'flash_error');
			}
		}
	}

	/**
	 * 
	 * @param id    The User Id
	 */
	public function edit($id) {
		// Set the user id
		$this->User->id = $id;
		
		// Check if the user exists
		if (!($this->User->exists())) {
			// Throw an exception
			throw new NotFoundException(__('Usuário inválido'));
		}
		
		// Check if it is a post or a put http method
		if ($this->request->is('post') || $this->request->is('put')) {
			// Persist the user
			if ($this->User->save($this->request->data)) {
				// Show a user message
				$this->Session->setFlash(__('O usuário foi salvo'), 'flash_success');
		
				// Redirect to the index function
				$this->redirect(array('action' => 'index'));
			} else {
				// Show a user message
				$this->Session->setFlash(__('O usuário não pode ser salvo. Por favor, tente de novo'), 'flash_error');
			}
		} else {
			// Retreive the requested data
			$this->request->data = $this->User->read(null, $id);
				
			// Clear the data
			unset($this->request->data['User']['password']);
		}
	}

	/**
	 * 
	 * @param id    The User Id
	 */
	public function delete($id) {
		// Check if the request is an post http method
		if (!$this->request->is('post')) {
			// Throw an exception
			throw new MethodNotAllowedException();
		}
		
		// Set the user id
		$this->User->id = $id;
		
		// Check if the user exists
		if (!$this->User->exists()) {
			// Throw an exception
			throw new NotFoundException(__('Usuário inválido'));
		}
		
		// Delete the user
		if ($this->User->delete()) {
			// Show a user message
			$this->Session->setFlash(__('Usuário apagado'), 'flash_success');
				
			// Redirect to the index function
			$this->redirect(array('action' => 'index'));
		}
		
		// Show a user message
		$this->Session->setFlash(__('O usuário não foi apagado'), 'flash_error');
		
		// Redirect to the index function
		$this->redirect(array('action' => 'index'));
	}

	/**
	 * 
	 * @param id    The User Id
	 */
	public function view($id) {
		// Set the id to the user object
		$this->User->id = $id;
		
		// Check if the user exists
		if (!($this->User->exists())) {
			// Throw an exception
			throw new NotFoundException(__('Usuário inválido'));
		}
		
		// Read the user object properties and show them
		$this->set('user', $this->User->read(null, $id));
	}

	public function jsonlist() {
		// To disable the layout
		$this->layout = null;
		
		// Set the json content to the json object
		$this->set('json', $this->User->find('all'));
		
		// Call the view
		$this->render('../Elements/json');
	}
	
	/**
	 *
	 * @param id    The User Id
	 */
	public function jsonview($id) {
		// To disable the layout
		$this->layout = null;
		
		// Set the id to the user object
		$this->User->id = $id;
		
		// Check if the user exists
		if (!($this->User->exists())) {
			// Throw an exception
			throw new NotFoundException(__('Usuário inválido'));
		}
	
		// Set the json content to the json object
		$this->set('json', $this->User->read(null, $id));
	
		// Call the view
		$this->render('../Elements/json');
	}

	public function login() {
		// TODO: Test this.
		// Set the request parameters (simulate a http post from a form)
		//$this->request->data['User']['username'] = 'Administrador';
		//$this->request->data['User']['password'] = '123';
		
		// Perfor the login
		if ($this->Auth->login()) {
			// If logged go to the page
			$this->redirect($this->Auth->redirect());
		} else {
			// Show a user message
			$this->Session->setFlash(__('Login ou senha inválido, tente de novo'), 'flash_error');
		}
	}

	public function logout() {
		// Redirect to the logout page
		$this->redirect($this->Auth->logout());
	}

}
?>